package com.health.userbackstage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.userbackstage.mapper.SetMealMapper;
import com.health.userbackstage.pojo.SetMeal;
import com.health.userbackstage.pojo.relation.SetMealCheckGroup;
import com.health.userbackstage.service.OrderService;
import com.health.userbackstage.service.relationservice.SetMealCheckGroupService;
import com.health.userbackstage.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ASUS
 * @Date 2023/8/18 21:19
 * @Version 1.0
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {

    @Autowired
    private SetMealMapper smm;

    @Autowired
    private SetMealCheckGroupService smcgs;

    @Autowired
    private OrderService os;

    @Override
    @Transactional(rollbackFor = Exception.class)
//  增加时根据数组添加检查组 一个事务
    public String add(SetMeal setMeal) {
        String result = judgeRepeat(setMeal);
        if(result.equals("成功")) {
            smm.add(setMeal);
            List<Integer> checkGroupIds = setMeal.getCheckGroupIds();
            for (Integer num : checkGroupIds) {
                SetMealCheckGroup smcg = new SetMealCheckGroup();
                smcg.setCheckGroupId(num);
                smcg.setSetMealId(setMeal.getId());
                smcgs.save(smcg);
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(SetMeal setMeal) {
        String result = editJudgeRepeat(setMeal);
        if(result.equals("成功")) {
//            先修改
            smm.updateById(setMeal);

//            方法一 删除全部旧数据 遍历添加新数据
            smcgs.delBySetMealId(setMeal.getId());
            smcgs.add(setMeal.getId(),setMeal.getCheckGroupIds());

//            方法二 遍历找到重复元素 对不重复的部分处理
////            修改前的数组
//            List<Integer> oldCheckGroupIds = smm.getCheckGroupIds(setMeal.getId());
////            修改后的id数组
//            List<Integer> newCheckGroupIds = setMeal.getCheckGroupIds();
//            List<Integer> repeatList = new ArrayList<>();
//            for (Integer num1:oldCheckGroupIds) {
//                for (Integer num2:newCheckGroupIds) {
//                    if(num1==num2){
//                        repeatList.add(num1);
//                    }
//                }
//            }//循环结束
//            for (Integer num:repeatList) {
//                oldCheckGroupIds.remove((Integer) num);
//                newCheckGroupIds.remove((Integer) num);
//            }//移除全部重复元素
////            旧删新增
//            for (Integer num:oldCheckGroupIds) {
//                smcgs.del(setMeal.getId(),num);
//            }
//            for (Integer num:newCheckGroupIds) {
//                smcgs.add(setMeal.getId(),num);
//            }
        }
        return result;
    }

    @Override
    public List<Integer> getCheckGroupIds(Integer id) {
        return smm.getCheckGroupIds(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void del(SetMeal setMeal) {
        smm.deleteById(setMeal);
        smcgs.delBySetMealId(setMeal.getId());
        os.delBySetMealId(setMeal.getId());
    }

    @Override
    public List<SetMeal> getAllSetMealReport() {
        return smm.getAllSetMealReport();
    }

    @Override
    public List<SetMeal> getSetMealReport() {
        return smm.getSetMealReport();
    }

    public  String judgeRepeat(SetMeal setMeal){
        String code = setMeal.getCode();
        String helpCode = setMeal.getHelpCode();
        String name = setMeal.getName();
        List<String> codes = smm.findCodes();
        List<String> helpCodes = smm.findHelpCodes();
        List<String> names = smm.findNames();
        for (String str:codes){
            if(str.equals(code)){
                return "不可创建重复编码";
            }
        }
        for (String str:helpCodes){
            if(str.equals(helpCode)){
                return "不可创建重复辅助编码";
            }
        }
        for (String str:names){
            if(str.equals(name)){
                return "不可创建重复名称";
            }
        }
        return "成功";
    }

    public  String editJudgeRepeat(SetMeal setMeal){
        SetMeal oldSetMeal = smm.selectById(setMeal.getId());
        String code = setMeal.getCode();
        String helpCode = setMeal.getHelpCode();
        String name = setMeal.getName();
        List<String> codes = smm.findCodes();
        List<String> helpCodes = smm.findHelpCodes();
        List<String> names = smm.findNames();
        for (String str:codes){
            if(str.equals(code)&&!str.equals(oldSetMeal.getCode())){
                return "不可创建重复编码";
            }
        }
        for (String str:helpCodes){
            if(str.equals(helpCode)&&!str.equals(oldSetMeal.getHelpCode())){
                return "不可创建重复辅助编码";
            }
        }
        for (String str:names){
            if(str.equals(name)&&!str.equals(oldSetMeal.getName())){
                return "不可创建重复名称";
            }
        }
        return "成功";
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void testTrans() {
//        SetMeal sm= new SetMeal();
//        sm.setCode("1");
//        sm.setAge("1");
//        sm.setPrice(99.3);
//        sm.setHelpCode("11");
//        sm.setName("zs");
//        sm.setSex("1");
//        smm.insert(sm);
//        int a=1/0;
//    }
}
